﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ria;

using ITSSP.Models;
using ITSSP.Views.ChangePassword;
using ITSSP.Web.Models;
using ITSSP.Web.Services;

namespace ITSSP.Views
{
	public partial class ChangePasswordPage : Page
	{
		private ChangePasswordInfo changePasswordInfo = new ChangePasswordInfo();
		private MessageWindow messagingWindow = new MessageWindow();

		public ChangePasswordPage()
		{
			InitializeComponent();

			this.changePasswordForm.CurrentItem = changePasswordInfo;
		}

		/// <summary>
		///     Handles <see cref="DataForm.AutoGeneratingField"/>. Adds the necessary event listeners
		///     so that the OK button will only be enabled when all fields are filled
		/// </summary>
		private void ChangePasswordForm_OnAutoGeneratingField(object sender, DataFormAutoGeneratingFieldEventArgs e)
		{
			if (e.Field.Content is PasswordBox) {
				((PasswordBox)e.Field.Content).PasswordChanged += EnableOrDisableOKButton;
			}
		}

		/// <summary>
		///     Enables the OK button if all fields are not empty, disable it otherwise
		/// </summary>
		private void EnableOrDisableOKButton(object sender, EventArgs e)
		{
			this.changePassword.IsEnabled = !(
				String.IsNullOrEmpty(((PasswordBox)this.changePasswordForm.Fields["OldPassword"].Content).Password) ||
				String.IsNullOrEmpty(((PasswordBox)this.changePasswordForm.Fields["Password"].Content).Password) ||
				String.IsNullOrEmpty(((PasswordBox)this.changePasswordForm.Fields["PasswordConfirmation"].Content).Password));
		}

		private void ChangePassword_Click(object sender, RoutedEventArgs e)
		{
			// If there was a validation error in a previously password change attempt, clear it
			this.changePasswordForm.ValidationSummary.Errors.Clear();

			if (this.changePasswordForm.ValidateItem()) {
				this.messagingWindow.Title = "Processing...";
				this.messagingWindow.Text = "Please wait.";
				this.messagingWindow.Symbol = null;
				this.messagingWindow.OkVisible = false;
				this.messagingWindow.Show();

				UserContext context = new UserContext();
				InvokeOperation op = context.ChangePassword(changePasswordInfo.OldPassword, changePasswordInfo.Password);
				op.Completed += changePassword_Completed;
			}
		}

		void changePassword_Completed(object sender, EventArgs e)
		{
			this.changePasswordInfo.Reset();
			this.messagingWindow.Close();

			InvokeOperation op = sender as InvokeOperation;
			if (op.HasError) {
				this.messagingWindow.Title = "Error";
				this.messagingWindow.Text = op.Error.Message;
				this.messagingWindow.Symbol = MessageWindow.DialogSymbol.CriticalError;
				op.MarkErrorAsHandled();
			} else {
				this.messagingWindow.Title = "Success";
				this.messagingWindow.Text = "Successfully changed your password.";
				this.messagingWindow.Symbol = MessageWindow.DialogSymbol.Success;
			}
			this.messagingWindow.OkVisible = true;
			this.messagingWindow.Show();
		}
	}
}
